package com.bjsj.pro.tools;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.util.Log;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class UpdateHelper {
    private static final String TAG = "UpdateHelper";
    private static final String FILE_PROVIDER_AUTHORITY = "com.example.myapplication.fileprovider";
    private static final String DATA_AND_TYPE = "application/vnd.android.package-archive";

    public static boolean checkForUpdated(Context context) {
        boolean updated = false;
        try {
            int currentVersion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        //TODO 获取服务端最新版本号，与currentVersion做比较
        return updated;
    }

    public static void autoInstallUpdate(final Context context,String updateurl) {
        OkGo.<File>get(updateurl).tag(context).execute(new FileCallback() {

            @Override
            public void onSuccess(File file1, Call call, Response response) {
                File file = file1;
                setPermission(file.getPath());
                installApk(context, file.getPath());
            }



        });
        new AlertDialog.Builder(context).setTitle("更新提示")
                .setMessage("app正在下载，下载完成自动安装,请勿退出")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface,int i) {

                    }
                })
                .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
//                        UpdateHelper.autoInstallUpdate(base,data.getData().getDownloadUrl());

                    }
                }).show();
    }

    private static void installApk(Context context, String apkPath) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        File file = new File(apkPath);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
            Uri apkUri = FileProvider.getUriForFile(context, FILE_PROVIDER_AUTHORITY, file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, DATA_AND_TYPE);
        } else {
            Uri apkUri = Uri.fromFile(file);
            intent.setDataAndType(apkUri, DATA_AND_TYPE);
        }
        context.startActivity(intent);
    }

    private static void setPermission(String filePath) {
        String command = "chmod " + "777" + " " + filePath;
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}